package com.infinitus.multids;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.infinitus.multids.domain.primary.ContentInfo;
import com.infinitus.multids.domain.secondary.ProductInfo;
import com.infinitus.multids.service.RedisService;
import com.infinitus.multids.task.FetchCatDataTask;



@RestController
@EnableAutoConfiguration
@EnableScheduling
@ComponentScan(basePackages={"com.infinitus.multids.*"})
@RequestMapping("/multids")
public class App {
	@Autowired
    private RedisService redisService;
	
	@Autowired
	private FetchCatDataTask fetchCatDataTask;
	
	
	
	@RequestMapping("/h")
	@Cacheable(value = "function", keyGenerator = "keyGenerator")
	public String getSessionId(HttpSession session) {
		redisService.set("123", "测试");
		System.out.println("进入了方法");
		String string = redisService.get("123").toString();
		return string;
	}
	
	@RequestMapping("/g")
	public String getCacheId(HttpSession session) {
		System.out.println("进入了方法");
		String string = redisService.get("123").toString();
		return string;
	}
	
	
	@RequestMapping("/loadData")
	@ResponseBody
	public Object loadData() {
		fetchCatDataTask.loadProductInfo();
		fetchCatDataTask.loadContentInfo();
		Map<String , String> map =new HashMap<String, String>();
		map.put("productTitle", ProductInfo.TITLE);
		map.put("contentName", ContentInfo.NAME);
		map.put("contentTitle", ContentInfo.TITLE);
		/*String [] arr = new String[]{ContentInfo.TITLE,ContentInfo.NAME,ProductInfo.TITLE };
		for (int i = 0; i < arr.length; i++) {
			Set<Serializable> dateKeys = redisService.keys(arr[i]+"*");
			for (Serializable s : dateKeys) {
				String key = (String) s;
				System.out.println(key + ": "
						+ redisService.get((String) key));
			}
		}*/
		return map;
	}
	
	@RequestMapping("/getContent")
	@ResponseBody
	public Object getContent(String id) {
		Map<String, String> resMap =  new HashMap<String, String>();
		if(id == null || "".equals(id.trim())){
			resMap.put("status", "error");
			resMap.put("reson", "id cannot be null");
			return resMap;
		}
		try{
			resMap = fetchCatDataTask.getContent(id);
		}catch(Exception e){
			resMap.put("status", "error");
			resMap.put("reson", e.getMessage());
		}
		return resMap;
	}
	
	@RequestMapping("/getProduct")
	@ResponseBody
	public Object getProduct(String id) {
		Map<String, String> resMap =  new HashMap<String, String>();
		if(id == null || "".equals(id.trim())){
			resMap.put("status", "error");
			resMap.put("reson", "id cannot be null");
			return resMap;
		}
		try{
			resMap = fetchCatDataTask.getProduct(id);
		}catch(Exception e){
			resMap.put("status", "error");
			resMap.put("reson", e.getMessage());
		}
		return resMap;
	}
	
	

	public static void main(String[] args) {
		System.out.println("Hello World ! App!");
		SpringApplication.run(App.class, args);
		// SpringApplication.run(UserController.class, args);
	}
}
